<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

///home/epascuzzo/public-html/chamilo/user/php/webservices
include('../nusoap-0.9.5/lib/nusoap.php');


ini_set('max_execution_time', - 1);
ini_set('memory_limit', - 1);
$time_start = microtime(true);
/*
 * Change to the location of the .csv file which you wish to use.
 */
//$file = dirname(__FILE__) . '/user_import.csv';
$user = array(
    'firstname'              => 'Jon',
    'lastname'               => 'Brion',
    'status'                 => '5',
    // 5 STUDENT - 1 TEACHER
    'email'                  => 'jon@example.com',
    'loginname'              => 'PEPE',
    'password'               => '12345678',
    // encrypted using sha1
    'encrypt_method'         => 'sha1',
    'language'               => 'english',
    'official_code'          => 'official',
    'phone'                  => '00000000',
    'expiration_date'        => '0000-00-00',
    'original_user_id_name'  => $user_field,
    // the extra user field that will be automatically created in the user profile see: main/admin/user_fields.php
    'original_user_id_value' => $random_user_id,
    // third party user id
    'secret_key'             => $secret_key,
    //Extra fields
    'extra'                  => array(
        array('field_name' => 'ruc', 'field_value' => '123'),
        array('field_name' => 'DNI', 'field_value' => '4200000')
    ),
);



//$client = new soapclient("http://http://localhost/chamilo/user/php/webservices/webservices_user.class.php?wsdl");
$location = 'http://http://localhost/chamilo/user/php/webservices/webservices_user.class.php?wsdl';
$client = new nusoap_client($location, 'wsdl');
$hash = '';


function update_user($user)
{
    global $hash, $client;
    log_message('Updating user ' . $user['username']);
    
    /*
     * If the hash is empty, request a new one. Else use the existing one.
     * Expires by default after 10 min.
     */
    
    $hash = ($hash == '') ? login() : $hash;
    
    $result = $client->call('WebServicesUser.update_user', array('input' => $user, 'hash' => $hash));
    if ($result == 1)
    {
        log_message(print_r('User successfully updated', true));
    }
    else
        log_message(print_r($result, true));
}


function login()
{
    global $client;
    
    /* Change the username and password to the ones corresponding to  your database.
     * The password for the login service is :
     * IP = the IP as seen by the target Chamilo server from where the call to the webservice is made.
     * You can request this through e.g. http://www.whatsmyip.org/
     * PW = your hashed password from the db
     * The hashing algoritm needs to be the same as the one used on the Chamilo installation you're trying to communicate with.
     * When in doubt, ask the administrator of said installation.
     * $password = Hash(IP+PW) ;
     */
    $username = 'Samumon';
    $password = hash('sha1', '193.190.172.141' . hash('sha1', '60d9efdb7c'));
    
    /*
     * change location to server location for the wsdl
     */
    $login_client = new nusoap_client('http://demo2.chamilo.org/user/webservices/login_webservice.class.php?wsdl', 'wsdl');
    $result = $login_client->call('LoginWebservice.login', array('username' => $username, 'password' => $password));
    
    log_message(print_r($result, true));
    if (is_array($result) && array_key_exists('hash', $result))
        return $result['hash']; //hash 3
    

    return '';

}


function dump($value)
{
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}


function log_message($text)
{
    echo date('[H:m] ', time()) . $text . '<br />';
}


function debug($client)
{
    echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
    echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
    echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
}